import time
import random

from ascript.android.system import R
from ascript.android import action
from ascript.android import system

from .my_action import MY_ACTION
from .opencv_tool import OpenCVTool


class Controller:
    def __init__(self, next_index=1):

        self.round = 1

        self.action = MY_ACTION()

        self.opencv = OpenCVTool()

        self.next_index = next_index

        # 签到
        self.is_sign = False

        # 预约天天领金币
        self.is_reservation = False

        # 天天领金币
        self.is_receive_daily_gold = False

        # 看小说签到
        self.is_go_novel_sign = False

        # 去小岛
        self.is_go_island = False

        # 逛街
        self.is_go_shopping = False

        # 听书签到
        self.is_listening_book_sign = False

        # 领红包跳一下游戏
        self.is_receive_red = False

    def start_dy(self):
        # 根据包名启动,推荐使用
        print("启动抖音")
        system.open("com.ss.android.ugc.aweme.lite")
        time.sleep(5)

        count = 1
        while count <= 6:
            print(f"slide_screen_up: {count}")
            count += 1
            self.action.slide_screen_up()
            time.sleep(5)

        # 打开任务列表
        rel_x, rel_y = self.action.get_current_loc(540, 1960)
        action.click(rel_x, rel_y)
        time.sleep(5)
        return True

    def sign(self):
        if self.is_sign:
            return True
        flag = self.action.click_text("去签到")
        if not flag:
            print("未找到[去签到]")
            self.is_sign = True
            return False
        if flag:
            time.sleep(3)
            rel_x, rel_y = self.action.get_current_loc(494, 1535)
            self.action.click_(rel_x, rel_y)
            time.sleep(2)
            self.action.go_back()
            self.is_sign = True
            return True

    def reservation(self):
        if self.is_reservation:
            return True
        flag, loc = self.opencv.find_img(R.img("reservation[quyuyue].png"))
        if not flag:
            print("未找到[reservation[quyuyue]]")
            return False
        rel_x, rel_y = self.action.get_offset_loc(loc[0], loc[1], 60, 23)
        self.action.click_(rel_x, rel_y)
        time.sleep(3)
        rel_x, rel_y = self.action.get_current_loc(533, 1374)
        self.action.click_(rel_x, rel_y)
        time.sleep(3)
        self.action.go_back()
        self.is_reservation = True
        return True

    def receive_(self):
        # 去领取
        flag, loc = self.opencv.find_img(R.img("receive_[qulingqu].png"))
        if not flag:
            print("未找到[receive_[qulingqu]]")
            return False
        rel_x, rel_y = self.action.get_offset_loc(loc[0], loc[1], 60, 20)
        self.action.click_(rel_x, rel_y)
        time.sleep(3)

        # 天天预约金币
        flag, loc = self.opencv.find_img(R.img("receive_daily_reservation[yijianlingqu].png"))
        if flag:
            print("领取天天预约金币")
            rel_x, rel_y = self.action.get_offset_loc(loc[0], loc[1], 132, 48)
            self.action.click_(rel_x, rel_y)
            time.sleep(3)
            self.action.go_back()
            return True

        # 天天领金币
        if not self.is_receive_daily_gold:
            # 判断是否已领过
            flag, loc = self.opencv.find_img(R.img("receive_daily_gold[yilingqu].png"))
            if flag:
                print("已领取天天领金币")
                self.action.go_back()
                time.sleep(3)
                self.action.go_back()
                self.is_receive_daily_gold = True
                return True
            # 领取金币
            flag, loc = self.opencv.find_img(R.img("receive_daily_gold[lingqu].png"))
            if flag:
                print("领取天天领金币")
                rel_x, rel_y = self.action.get_offset_loc(loc[0], loc[1], 36, 27)
                self.action.click_(rel_x, rel_y)
                time.sleep(3)
                self.action.go_back()
                time.sleep(3)
                self.action.go_back()
                self.is_receive_daily_gold = True
                return True

        # 领取餐补
        flag, loc = self.opencv.find_img(R.img("receive_eat[lingqu].png"))
        if flag:
            print("领取餐补")
            rel_x, rel_y = self.action.get_offset_loc(loc[0], loc[1], 72, 38)
            self.action.click_(rel_x, rel_y)
            time.sleep(3)
            self.action.go_back()
            return True

        # 看广告
        if self.action.has_text("广告"):
            # 看视频
            flag, loc = self.opencv.find_img(R.img("open_box[see_vedio].png"))
            if not flag:
                self.action.go_back()
                return True
            x, y = self.action.get_offset_loc(loc[0], loc[1], 121, 37)
            self.action.click_(x, y)
            time.sleep(3)

            count = 1
            while count <= 7:
                print(f"[see_vedio],看{count}*5秒")
                count += 1
                time.sleep(5)
            self.action.go_back()
            time.sleep(2)
            # 领取
            x, y = self.action.get_current_loc(838, 101)
            self.action.click_(x, y)
            time.sleep(3)
            # 确定
            x, y = self.action.get_current_loc(533, 1213)
            self.action.click_(x, y)
            time.sleep(3)
            # 再看
            count = 1
            while count <= 7:
                print(f"[see_vedio2],看{count}*5秒")
                count += 1
                time.sleep(5)
            # 再领取
            x, y = self.action.get_current_loc(838, 101)
            self.action.click_(x, y)
            time.sleep(3)

            # 评价
            x, y = self.action.get_current_loc(491, 1437)
            self.action.click_(x, y)

        self.action.go_back()
        return False

    def open_box(self):
        flag, loc = self.opencv.find_img(R.img("open_box.png"))
        if not flag:
            print("未找到[open_box[dianjiling]]")
            return False
        time.sleep(3)
        x, y = self.action.get_offset_loc(loc[0], loc[1], 70, 27)
        self.action.click_(x, y)
        time.sleep(3)
        self.action.go_back()
        return True

    def open_box2(self):
        flag, loc = self.opencv.find_img(R.img("open_box2[kaibaoxiang].png"))
        if not flag:
            flag, loc = self.opencv.find_img(R.img("open_box2[kaibaoxiang]2.png"))
        if not flag:
            print("未找到[open_box2[kaibaoxiang]]")
            return False
        rel_x, rel_y = self.action.get_offset_loc(loc[0], loc[1], 67, 23)
        self.action.click_(rel_x, rel_y)
        time.sleep(5)

        # 看视频
        flag, loc = self.opencv.find_img(R.img("open_box[see_vedio].png"))
        if not flag:
            self.action.go_back()
            return True
        x, y = self.action.get_offset_loc(loc[0], loc[1], 121, 37)
        self.action.click_(x, y)
        time.sleep(3)

        count = 1
        while count <= 7:
            print(f"[see_vedio],看{count}*5秒")
            count += 1
            time.sleep(5)
        self.action.go_back()
        time.sleep(2)
        # 领取
        x, y = self.action.get_current_loc(838, 101)
        self.action.click_(x, y)
        time.sleep(3)
        # 确定
        x, y = self.action.get_current_loc(533, 1213)
        self.action.click_(x, y)
        time.sleep(3)
        # 再看
        count = 1
        while count <= 7:
            print(f"[see_vedio2],看{count}*5秒")
            count += 1
            time.sleep(5)
        # 再领取
        x, y = self.action.get_current_loc(838, 101)
        self.action.click_(x, y)
        time.sleep(3)

        # 评价
        x, y = self.action.get_current_loc(491, 1437)
        self.action.click_(x, y)
        return True

    def go_shopping(self):
        if self.is_go_shopping:
            return True
        flag, loc = self.opencv.find_img(R.img("go_shopping[quguangjie].png"))
        if not flag:
            print("未找到[go_shopping[quguangjie]]")
            return False
        rel_x, rel_y = self.action.get_offset_loc(loc[0], loc[1], 67, 23)
        self.action.click_(rel_x, rel_y)

        # 获取浏览时间
        time.sleep(3)
        flag, loc = self.opencv.find_img(R.img("go_shopping_[liulan].png"))
        if not flag:
            print("未找到[go_shopping_[liulan]]")
            return False
        rightdown_x, rightdown_y = self.action.get_offset_loc(loc[0], loc[1], 270, 260)
        see_time = self.action.find_time(loc, (rightdown_x, rightdown_y))
        print(f"[go_shopping]see_time: {see_time}")
        if see_time is None:
            self.action.go_back()
            self.is_go_shopping = True
            return False

        # 开始浏览
        total = see_time // 5 + 1
        count = 1
        while count <= total:
            print(f"slide_screen_up[{total}]: {count}")
            count += 1
            self.action.slide_screen_up()
            time.sleep(5)

        self.action.click_text("评价并收下金币")
        time.sleep(3)
        self.action.go_back()
        return True

    def go_island(self):
        if self.is_go_island:
            return True

        flag, loc = self.opencv.find_img(R.img("go_island.png"))
        if not flag:
            print("未找到[go_island.png]")
            return False
        rel_x, rel_y = self.action.get_offset_loc(loc[0], loc[1], 67, 18)
        self.action.click_(rel_x, rel_y)
        time.sleep(5)

        # 当日是否已完成
        flag, loc = self.opencv.find_img(R.img("go_island[jilu].png"))
        if flag:
            print("[go_island]当日已完成")
            self.is_go_island = True
            self.action.go_back()
            return False

        rel_x, rel_y = self.action.get_current_loc(533, 1837)
        self.action.click_(rel_x, rel_y)
        time.sleep(3)
        self.action.go_back()
        return True

    def go_novel(self):
        flag, loc = self.opencv.find_img(R.img("go_novel[kanxiaoshuo].png"))
        if not flag:
            print("未找到[go_novel[kanxiaoshuo]]")
            return False
        rel_x, rel_y = self.action.get_offset_loc(loc[0], loc[1], 62, 21)
        self.action.click_(rel_x, rel_y)
        time.sleep(5)

        # 看小说一分钟
        flag = self.action.click_text("9.4分")
        if flag:
            count = 1
            while count <= 12:
                print(f"[go_novel],看{count}*5秒")
                count += 1
                self.action.slide_screen_left()
                time.sleep(5)
            time.sleep(2)
            # 收藏提示返回
            self.action.go_back()
            time.sleep(2)
            # 返回小说主界面
            self.action.go_back()
            time.sleep(5)

        # 看小说任务界面
        flag, loc = self.opencv.find_img(R.img("go_novel[hongbao].png"))
        if not flag:
            flag, loc = self.opencv.find_img(R.img("go_novel[hongbao]2.png"))
        if flag:
            rel_x, rel_y = self.action.get_offset_loc(loc[0], loc[1], 36, 51)
            self.action.click_(rel_x, rel_y)
            time.sleep(3)

            # 去签到
            if not self.is_go_novel_sign:
                flag, loc = self.opencv.find_img(R.img("go_novel[sign_see].png"))
                if flag:
                    # 已经签过了
                    self.is_go_novel_sign = True
                else:
                    print("go_novel->去签到")
                    rel_x, rel_y = self.action.get_current_loc(933, 1139)
                    self.action.click_(rel_x, rel_y)
                    time.sleep(3)
                    # 立即签到
                    rel_x, rel_y = self.action.get_current_loc(522, 1399)
                    self.action.click_(rel_x, rel_y)
                    self.is_go_novel_sign = True
                    time.sleep(3)
                    self.action.go_back()
                    time.sleep(3)

            flag, loc = self.opencv.find_img(R.img("go_novel[kanxiaoshuo]2.png"))
            if not flag:
                # 去领取
                print("go_novel->去领取")
                rel_x, rel_y = self.action.get_current_loc(930, 1328)
                self.action.click_(rel_x, rel_y)
                time.sleep(3)
                # 看视频再领取
                rel_x, rel_y = self.action.get_current_loc(515, 1199)
                self.action.click_(rel_x, rel_y)
                time.sleep(35)
                rel_x, rel_y = self.action.get_current_loc(845, 101)
                self.action.click_(rel_x, rel_y)
                # 返回主界面
                time.sleep(3)
                self.action.go_back()
            else:
                self.action.go_back()
        # 返回任务列表
        time.sleep(3)
        self.action.go_back()
        return True

    def is_task_list(self):
        flag, loc = self.opencv.find_img(R.img("is_task_list[task].png"))
        if not flag:
            print("未找到[is_task_list[task].png]")
            print("准备重启应用")
            self.action.restart_app()
            time.sleep(5)
            self.start_dy()
        return flag

    def receive_red(self):
        if self.is_receive_red:
            return True

        flag, loc = self.opencv.find_img(R.img("receive_red[linghongbao].png"))
        if not flag:
            print("未找到[receive_red[linghongbao]]")
            return False
        rel_x, rel_y = self.action.get_offset_loc(loc[0], loc[1], 65, 24)
        self.action.click_(rel_x, rel_y)
        time.sleep(5)

        # 判断是否开始游戏
        flag, loc = self.opencv.find_img(R.img("receive_red[zuorenwu].png"))
        if flag:
            self.action.go_back()
            self.is_receive_red = True
            return True

        # 开始
        time.sleep(3)
        rel_x, rel_y = self.action.get_current_loc(544, 1749)
        count = 1
        while count <= 35:
            print(f"跳跃次数：{count}")
            self.action.click_(rel_x, rel_y)
            count += 1
            time.sleep(2)

        rel_x, rel_y = self.action.get_current_loc(515, 1230)
        self.action.click_(rel_x, rel_y)
        time.sleep(3)
        self.action.go_back()
        return True

    def listening_book(self):
        flag, loc = self.opencv.find_img(R.img("listening_book.png"))
        if not flag:
            print("未找到[listening_book[linghongbao]]")
            return False
        rel_x, rel_y = self.action.get_offset_loc(loc[0], loc[1], 65, 21)
        self.action.click_(rel_x, rel_y)
        time.sleep(5)

        # 听书一分钟
        rel_x, rel_y = self.action.get_current_loc(894, 1925)
        self.action.click_(rel_x, rel_y)
        count = 1
        while count <= 12:
            print(f"[listening_book],听{count}*5秒")
            count += 1
            time.sleep(5)
        time.sleep(2)
        # 返回主界面
        self.action.go_back()
        time.sleep(3)

        # 听书任务界面
        flag, loc = self.opencv.find_img(R.img("listening_book[task].png"))
        if not flag:
            flag, loc = self.opencv.find_img(R.img("listening_book[task]2.png"))
        if not flag:
            print("未找到[listening_book[task]]")
            self.action.go_back()
            return False
        rel_x, rel_y = self.action.get_offset_loc(loc[0], loc[1], 31, 38)
        self.action.click_(rel_x, rel_y)
        time.sleep(5)

        # 签到
        if not self.is_listening_book_sign:
            flag, loc = self.opencv.find_img(R.img("listening_book[sign].png"))
            if flag:
                rel_x, rel_y = self.action.get_offset_loc(loc[0], loc[1], 63, 33)
                self.action.click_(rel_x, rel_y)
                time.sleep(3)
                # 签到领金币
                rel_x, rel_y = self.action.get_current_loc(522, 1367)
                self.action.click_(rel_x, rel_y)
                self.is_listening_book_sign = True
                time.sleep(3)
            else:
                self.is_listening_book_sign = True

        # 立即领取
        flag, loc = self.opencv.find_img(R.img("listening_book[liji].png"))
        if flag:
            rel_x, rel_y = self.action.get_offset_loc(loc[0], loc[1], 82, 31)
            self.action.click_(rel_x, rel_y)
            time.sleep(3)

        # 返回任务主界面
        self.action.go_back()
        time.sleep(3)
        self.action.go_back()
        return True

    def go_mall(self):

        flag, loc = self.opencv.find_img(R.img("go_mall.png"))
        if not flag:
            print("未找到[go_mall.png]")
            return False
        rel_x, rel_y = self.action.get_offset_loc(loc[0], loc[1], 68, 26)
        self.action.click_(rel_x, rel_y)
        time.sleep(5)

        # 呆半分钟
        count = 1
        while count <= 7:
            print(f"[listening_book],看{count}*5秒")
            count += 1
            self.action.slide_screen_up(distance=60)
            time.sleep(5)

        # 返回主界面
        rel_x, rel_y = self.action.get_current_loc(533, 1956)
        self.action.click_(rel_x, rel_y)
        return True

    def do_round(self):
        print(f"当前任务轮次：{self.round}")
        if self.round % 5 == 0:
            print("准备重启应用")
            self.action.restart_app()
            time.sleep(5)
            self.start_dy()
        else:
            dis = 0
            value = random.randint(1, 4)
            if value == 1:
                dis = 40
            elif value == 2:
                dis = 50
            elif value == 3:
                dis = 60
            else:
                dis = 70
            self.action.slide_screen_up(distance=dis)
            time.sleep(3)
        self.round += 1

    def send_present(self):
        flag, loc = self.opencv.find_img(R.img("send_present.png"))
        if not flag:
            print("未找到[listening_book[linghongbao]]")
            return False
        rel_x, rel_y = self.action.get_offset_loc(loc[0], loc[1], 51, 18)
        self.action.click_(rel_x, rel_y)
        time.sleep(8)

        flag, loc = self.opencv.find_img(R.img("send_present[song].png"))
        if not flag:
            print("未找到[send_present[song].png]]")
            return False
        if flag:
            # 赠送小心心
            rel_x, rel_y = self.action.get_offset_loc(loc[0], loc[1], 47, 34)
            self.action.click_(rel_x, rel_y)
            time.sleep(5)
            self.action.go_back()
            time.sleep(3)
            self.action.go_back()
        return True
